﻿/**
*tab menu item class
*auth:lww
*version:1.0
*date:2014-10-11
*/
function TabMenuItem(context, name) {
    this.id = "";
    this.name = "TabMenuItem";
    this.version = "1.0.0";
    this.mContext = context;
    /**
    *item info
    */
    this.mText = name;
    this.mView = null;
    this.mIndex = -1; 
    this.mData = null;
}
TabMenuItem.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};
TabMenuItem.prototype.setId = function (ids) {
    this.id = ids;
};
TabMenuItem.prototype.setIndex = function (index) {
    this.mIndex = index;
};
TabMenuItem.prototype.invalidate = function () {
    if (this.mView == null) {
        this.mView = $c("li", this.getId());
        this.setName(this.getName());
        this.mView.title = this.getName();
        var o = this;
        this.mView.onclick = function () {
            if (o.mContext != null
                && typeof (o.mContext.setSelected) != 'undefined'
                && o.mIndex >= 0) {
                o.mContext.setSelected(o.mIndex);
            }
        };
        this.mView.onmouseover = function (e) { e = e ? e : event; addCss(this, "onFocus"); };
        this.mView.onmouseout = function (e) { e = e ? e : event; rmCss(this, "onFocus"); };
    }
    return this.mView;
};
TabMenuItem.prototype.view = function () {
    if (this.mView != null)
        return this.mView;
    return this.invalidate();
};
TabMenuItem.prototype.getName = function () {
    return this.mText;
};
TabMenuItem.prototype.setName = function (name) {
    if (this.mView != null) {
        this.mText = name;
        setChild(this.mView, setHtml(setCss($c("span"), "itemTitle") ,name));
    }
};
TabMenuItem.prototype.setClassName = function (name) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        setCss(this.mView, name);
    }
};
TabMenuItem.prototype.addClassName = function (name) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        addCss(this.mView, name);
    }
};
TabMenuItem.prototype.rmClassName = function (name) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        rmCss(this.mView, name);
    }
};
TabMenuItem.prototype.getData = function () {
    return this.mData;
};

/**
*tab menu view class
*auth:lww
*version:1.0
*date:2014-10-11
*/
function TabMenuView(context,data) {
    this.id = "";
    this.name = "TabMenuView";
    this.version = "1.0.0";
    this.mContext = context;
    /**
    *menu info
    */
    this.mView = null;
    this.mSelectedIndex = -1;
    this.mIndex = 0;
    this.mItemList = null;
    /**
    *menu data source
    */
    this.mData = data;
}
TabMenuView.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};
TabMenuView.prototype.setId = function (ids) {
    this.id = ids;
};
TabMenuView.prototype.invalidate = function () {
    if (this.mView == null) {
        this.mView = $c("ul", this.getId());
    }
    return this.mView;
};
TabMenuView.prototype.view = function () {
    if (this.mView != null)
        return this.mView;
    return this.invalidate();
};
TabMenuView.prototype.setClassName = function (name, flag) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        setCss(this.mView, name);
    }
};
TabMenuView.prototype.addItem = function (item) {
    this.invalidate();
    /**
    *add to list
    */
    if (this.mItemList == null) {
        this.mItemList = new Array();
        this.mIndex = 0;
    }

    if (item != null) {
        item.setIndex(this.mIndex);
        this.mItemList[this.mIndex++] = item;
        this.mView.appendChild(item.view());
    }
};
TabMenuView.prototype.getSize = function () {
    if (this.mItemList == null)
        return 0;
    else
        return this.mItemList.length;
};
TabMenuView.prototype.getItem = function (index) {
    if (this.getSize() > 0 && index >= 0 && index < this.getSize()) {
        return this.mItemList[index];
    }
    else {
        return null;
    }
};
TabMenuView.prototype.onSelected = function (index, menuItem, isSelected) {
    //add your code
};
TabMenuView.prototype.setSelected = function (index) {
    if(this.getSize() <= 0){
        return;
    }
    if (this.mSelectedIndex >= 0 && this.mSelectedIndex != index) {
        this.onSelected(this.mSelectedIndex, this.getItem(this.mSelectedIndex), false);
        this.getItem(this.mSelectedIndex).rmClassName("tabItemSelected");
    }
    if (this.mSelectedIndex != index) {
        this.mSelectedIndex = index;
        this.onSelected(this.mSelectedIndex, this.getItem(this.mSelectedIndex), true);
        this.getItem(this.mSelectedIndex).addClassName("tabItemSelected");
    }
};
TabMenuView.prototype.addMenuItem = function(menuInfo,className){
    var menuItem = new TabMenuItem(this, menuInfo.getName());
    menuItem.setClassName(className);
    menuItem.mData = menuInfo;
    this.addItem(menuItem);
    return menuItem;
};

markscript("TabMenuView");
//end TabMenuView.js